package cn.iocoder.yudao.module.shop.service.itemtimesrecord;

import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.shop.dal.dataobject.item.ShopItemDO;
import cn.iocoder.yudao.module.shop.service.item.ShopItemService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.itemtimesrecord.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemtimesrecord.ShopItemTimesRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.itemtimesrecord.ShopItemTimesRecordConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.itemtimesrecord.ShopItemTimesRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 项目次数变化记录 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopItemTimesRecordServiceImpl implements ShopItemTimesRecordService {

    @Resource
    private ShopItemTimesRecordMapper itemTimesRecordMapper;


    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private ShopItemService shopItemService;

    @Override
    public Long createItemTimesRecord(ShopItemTimesRecordCreateReqVO createReqVO) {
        // 插入
        ShopItemTimesRecordDO itemTimesRecord = ShopItemTimesRecordConvert.INSTANCE.convert(createReqVO);
        itemTimesRecordMapper.insert(itemTimesRecord);
        // 返回
        return itemTimesRecord.getId();
    }

    @Override
    public Long record(Long memberId, Long itemId, Integer change, Integer changeAfter, Integer price, String reason, Long tenantId){
        MemberUserRespDTO memberUserRespDTO = memberUserApi.getUser(memberId);
        if(memberUserRespDTO == null){
            throw exception(MEMBER_NOT_EXISTS);
        }

        ShopItemDO shopItemDO = shopItemService.getItem(itemId);
        if(shopItemDO == null){
            throw exception(ITEM_NOT_EXISTS);
        }

        ShopItemTimesRecordCreateReqVO reqVO = new ShopItemTimesRecordCreateReqVO();
        reqVO.setMemberId(memberId);
        reqVO.setNickname(memberUserRespDTO.getNickname());
        reqVO.setOptItemId(itemId);
        reqVO.setItemName(shopItemDO.getName());
        reqVO.setChangeValue(change);
        reqVO.setChangeAfter(changeAfter);
        reqVO.setPrice(price);
        reqVO.setReason(reason);
        reqVO.setTenantId(tenantId);

        return createItemTimesRecord(reqVO);
    }

    @Override
    public void updateItemTimesRecord(ShopItemTimesRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateItemTimesRecordExists(updateReqVO.getId());
        // 更新
        ShopItemTimesRecordDO updateObj = ShopItemTimesRecordConvert.INSTANCE.convert(updateReqVO);
        itemTimesRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteItemTimesRecord(Long id) {
        // 校验存在
        validateItemTimesRecordExists(id);
        // 删除
        itemTimesRecordMapper.deleteById(id);
    }

    private void validateItemTimesRecordExists(Long id) {
        if (itemTimesRecordMapper.selectById(id) == null) {
            throw exception(ITEM_TIMES_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ShopItemTimesRecordDO getItemTimesRecord(Long id) {
        return itemTimesRecordMapper.selectById(id);
    }

    @Override
    public List<ShopItemTimesRecordDO> getItemTimesRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return itemTimesRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopItemTimesRecordDO> getItemTimesRecordPage(ShopItemTimesRecordPageReqVO pageReqVO) {
        return itemTimesRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopItemTimesRecordDO> getItemTimesRecordList(ShopItemTimesRecordExportReqVO exportReqVO) {
        return itemTimesRecordMapper.selectList(exportReqVO);
    }

}
